package com.rabbiter.oes.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.oes.entity.Subject;
import com.rabbiter.oes.mapper.*;
import com.rabbiter.oes.service.SubjectService;
import com.rabbiter.oes.vo.SubjectListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    // 试卷
    @Autowired
    private ExamManageMapper examManageMapper;

    // 选择题
    @Autowired
    private MultiQuestionMapper multiQuestionMapper;

    // 判断题
    @Autowired
    private JudgeQuestionMapper judgeQuestionMapper;

    // 填空题
    @Autowired
    private FillQuestionMapper fillQuestionMapper;

    @Override
    public IPage<SubjectListVO> findPage(Page<SubjectListVO> page, String name) {
        name = ("@".equals(name) ? "" : name);
        return baseMapper.findPage(page, name);
    }

    @Override
    public List<Subject> findAll() {
        return baseMapper.selectList(Wrappers.lambdaQuery());
    }

    @Override
    public Long findCountByName(String name) {
        return baseMapper.findCountByName(name);
    }

    @Override
    public Subject findById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int deleteById(String id) {
        Long examCount = examManageMapper.selectCountBysubjectId(id);
        Assert.isTrue((examCount == 0), "该科目有试卷，不允许删除操作。");

        Long multiCount = multiQuestionMapper.selectCountBysubjectId(id);
        Assert.isTrue((multiCount == 0), "该科目有选择题，不允许删除操作。");

        Long judgeCount = judgeQuestionMapper.selectCountBysubjectId(id);
        Assert.isTrue((judgeCount == 0), "该科目有判断题，不允许删除操作。");

        Long fillCount = fillQuestionMapper.selectCountBysubjectId(id);
        Assert.isTrue((fillCount == 0), "该科目有填空题，不允许删除操作。");

        return baseMapper.deleteById(id);
    }

    @Override
    public int update(Subject subject) {
        return baseMapper.updateById(subject);
    }

    @Override
    public int add(Subject subject) {
        return baseMapper.insert(subject);
    }

}
